package com.jiaai.web.zfp_back.anchor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.exception.ServerException;
import com.jiaai.gift.api.IGiftHisApi;
import com.jiaai.hot.api.HotApi;
import com.jiaai.live.api.IAnchorApi;
import com.jiaai.live.model.AnchorQueryVO;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * class_name:ZBAnchorController
 * describe:TODO
 * create_user:张昉平
 * create_date:2017/12/19
 * create_date:10:50
 */
@RestController
@RequestMapping("/api/v1/admin/anchor")
public class ZBAnchorController {

    @Reference
    private IAnchorApi iAnchorApi;
    @Reference
    private IGiftHisApi iGiftHisApi;
    @Reference
    private HotApi hotApi;

    @GetMapping("/list")
    public Object list(@RequestParam Integer curr, AnchorQueryVO vo){
        return RestResultGenerator.genSuccessResult(iAnchorApi.getAll(curr, vo));
    }

    @PostMapping("/change")
    public Object change(@RequestParam Integer STATUS, @RequestParam String anchorId){
        try {
            iAnchorApi.checkAnchorStatus(anchorId,STATUS);
            return RestResultGenerator.genSuccessResult("操作成功");
        } catch (ServerException e) {
            e.printStackTrace();
            throw new RuntimeException("失败");
        }
    }
    @PostMapping("/changePassStatus")
    public Object checkAnchorPassStatus(@RequestParam Integer passStatus, @RequestParam String anchorId){
        try {
            iAnchorApi.checkAnchorPassStatus(anchorId,passStatus);
            return RestResultGenerator.genSuccessResult("操作成功");
        } catch (ServerException e) {
            e.printStackTrace();
            throw new RuntimeException("失败");
        }
    }

    @PostMapping("/saveRefuse")
    public Object saveRefuse(@RequestParam String refuse, @RequestParam String anchorId,Integer passStatus){
        try {
            iAnchorApi.saveRefuse(anchorId,refuse,passStatus);
            return RestResultGenerator.genSuccessResult("保存成功");
        } catch (ServerException e) {
            e.printStackTrace();
            throw new RuntimeException("失败");
        }
    }

    @PostMapping("/heat")
    public Object heat(@RequestParam String anchorId, @RequestParam Integer flag){
        if (flag == 1){
            hotApi.insertHot(anchorId);
            return RestResultGenerator.genSuccessResult("推荐成功");
        }else {
            hotApi.deleteHotLive(anchorId);
            return RestResultGenerator.genSuccessResult("下架成功");
        }
    }

    @GetMapping("/oneInfo")
    public Object oneInfo(@RequestParam String anchorId){
        Map map = iAnchorApi.oneInfo(anchorId);
        map.put("sendGift", iGiftHisApi.getOneAllHis(map.get("anchorId").toString(), 1));//送出去的礼物
        map.put("getGift", iGiftHisApi.getOneAllHis(anchorId, 2));//收到的礼物
        return RestResultGenerator.genSuccessResult(map);
    }

    @GetMapping("/anchorLive")
    public Object myLive(String anchorId, String startTime, String endTime, @RequestParam(required = false, defaultValue = "1") Integer curr){
        return RestResultGenerator.genSuccessResult(iAnchorApi.myLive(curr, anchorId, startTime, endTime));
    }

    @GetMapping("/newnum")
    public Object tongjiNewAnchor(){
        TreeMap map = iAnchorApi.tongjiNewAnchor();
        List<Map<String, String>> list = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

        List temp = new ArrayList(map.values());

        for (int i = 0; i < temp.size(); i++) {
            String date = sdf.format(new Date((new Date().getTime()-(i * 1000 * 60 * 60 * 24))));
            Map mm = new HashMap();
            mm.put("date", date);
            mm.put("value", temp.get(i));
            list.add(mm);
        }

        Collections.reverse(list);
        return RestResultGenerator.genSuccessResult(list);
    }

    @GetMapping("/ranking")
    public Object ranking(@RequestParam String type){
        try {
            return RestResultGenerator.genSuccessResult(iAnchorApi.findRanking(type));
        } catch (ServerException e) {
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("");
        }
    }
}
